<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>简单聊天室</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 20px;
        }

        #chat-container {
            width: 100%;
            max-width: 800px;
            margin: 0 auto;
        }

        #message-list {
            height: 300px;
            overflow-y: scroll;
            padding: 10px;
            border: 1px solid #ccc;
            margin-bottom: 10px;
        }

        #input-container {
            display: flex;
        }

        #input-container input {
            flex: 1;
            padding: 5px;
            border: 1px solid #ccc;
            border-radius: 4px;
        }

        #input-container button {
            margin-left: 10px;
            padding: 5px 10px;
            background-color: #4CAF50;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        }
    </style>
</head>

<body>
    <div id="chat-container">
        <div id="message-list"></div>
        <div id="input-container">
            <input type="text" id="message-input" placeholder="输入消息...">
            <button onclick="sendMessage()">发送</button>
        </div>
    </div>
    <script>
        function sendMessage() {
            const messageInput = document.getElementById('message-input');
            const message = messageInput.value;
            if (message) {
                sendApi(message);
                messageInput.value = ''; // 清空输入框                
            } else {
                alert('请输入消息内容！'); // 提示用户输入消息内容，可根据需求自定义提示方式或样式
            }
        }
        function sendApi(message) {
            console.log('Sending message:', message);
            // 构造请求的URL，包含查询参数message
            // const message = 'Hello, this is a message from the frontend!';
            const encodedMessage = encodeURIComponent(message); // 对消息进行URL编码
            const url = `http://localhost:5000/chat?mmm=${encodedMessage}`;
            const messageList = document.getElementById('message-list');
            const messageElement = document.createElement('div');
            // 使用fetch API发送GET请求
            fetch(url, {
                method: 'GET', // 默认就是 GET，这里明确指定以增加清晰度
                headers: {
                    // 可以在这里设置请求头，但不要包含触发 WebSocket 升级的头部
                }
            })
                .then(response => response.json())
                .then(data => {
                    messageElement.textContent = data.content;
                    messageList.appendChild(messageElement);
                    messageList.scrollTop = messageList.scrollHeight; // 滚动到最底部显示最新消息
                    console.log('Received messages:', data); // 在控制台打印接收到的消息历史
                })
                .catch(error => {
                    console.error('There was a problem with the fetch operation:', error);
                });
        }
    </script>
</body>

</html>